<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    {{pageTitle}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="container" :style="{'min-height': containerHeight + 'px'}">
            <div class="handle-box flex flex-center">
                <el-button type="primary" icon="el-icon-folder" @click="submitForm('ruleForm')">保存</el-button>
            </div>

            <div class="table-form-box">
                <h3 class="h3">离职申请单</h3>
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                    <table border="0" width="100%" cellpadding="0" cellspacing="1">
                        <tbody>
                            <tr class="tr-title">
                                <td colspan="6">基本信息</td>
                            </tr>
                            <tr>
                                <td align="right">离职编号</td>
                                <td>
                                    <el-form-item prop="id">
                                        <el-input
                                            placeholder="请输入内容"
                                            v-model="ruleForm.id"
                                            size="small"
                                            disabled
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right">
                                    <span class="color-ff2200">*</span>姓名:
                                </td>
                                <td>
                                    <el-form-item prop="resignationUserName">
                                        <div @click="showTreeDialog('姓名')">
                                            <el-input
                                                placeholder="请输入"
                                                v-model="ruleForm.resignationUserName"
                                                size="small"
                                                readonly
                                            >
                                                <i slot="suffix" class="el-icon-search"></i>
                                            </el-input>
                                        </div>
                                    </el-form-item>
                                </td>

                                <td align="right">
                                    <span class="color-ff2200">*</span>申请时间:
                                </td>
                                <td>
                                    <el-form-item prop="applyTime">
                                        <el-date-picker
                                            style="width:99%"
                                            v-model="ruleForm.applyTime"
                                            align="right"
                                            type="date"
                                            placeholder="选择日期"
                                        ></el-date-picker>
                                    </el-form-item>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">所在部门</td>
                                <td>
                                    <el-form-item prop="departName">
                                        <el-input
                                            v-model="ruleForm.departName"
                                            size="small"
                                            disabled
                                        ></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right">所属岗位</td>
                                <td>
                                    <el-form-item prop="jobRole">
                                        <el-input v-model="ruleForm.jobRole" size="small" disabled></el-input>
                                    </el-form-item>
                                </td>
                                <td align="right">
                                    <span class="color-ff2200">*</span>离职时间:
                                </td>
                                <td>
                                    <el-form-item prop="resignationTime">
                                        <el-date-picker
                                            style="width:99%"
                                            v-model="ruleForm.resignationTime"
                                            align="right"
                                            type="date"
                                            placeholder="选择日期"
                                        ></el-date-picker>
                                    </el-form-item>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <span class="color-ff2200">*</span>离职原因:
                                </td>
                                <td>
                                    <el-form-item prop="resignationReason">
                                        <el-input v-model="ruleForm.resignationReason" size="small"></el-input>
                                    </el-form-item>
                                </td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td align="right">备注:</td>
                                <td colspan="6">
                                    <el-form-item prop="remarks">
                                        <el-input v-model="ruleForm.remarks" type="textarea"></el-input>
                                    </el-form-item>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </el-form>
            </div>

            <!-- 上传附件 -->
            <upload-files :files="fileList" @fun="dealFiles" v-if="flag"></upload-files>
        </div>
        <!-- 用户树搜索 -->
        <tree-search
            :isShow="showSearch"
            :dialogTitle="searchDialogTit"
            @edit="searchBack"
            @close="closeTreeDialog"
        ></tree-search>
    </div>
</template>

<script>
import appMain from '../../../utils/app_main';
export default {
    data() {
        return {
            pageTitle: '',
            containerHeight: '',
            searchDialogTit: '',
            showSearch: false,
            ruleForm: {
                applyTime: '', //离职申请时间
                departName: '', //部门名称 离职人信息回显
                departNo: '', //部门id  离职人信息回显
                jobRole: '', //所属岗位    离职人信息回显
                jobRoleId: '', //所属岗位ID   离职人信息回显
                remarks: '', //备注信息
                resignationReason: '', //离职原因
                resignationTime: '', //离职日期
                resignationUserId: '', //离职人id    接口查询
                resignationUserName: '' //离职人     接口查询
            },
            rules: {
                resignationUserName: [{ required: true, message: '请选择', trigger: 'change' }],
                resignationTime: [{ required: true, message: '请选择时间', trigger: 'blur' }],
                applyTime: [{ required: true, message: '请选择时间', trigger: 'blur' }],
                resignationReason: [{ required: true, message: '请输入内容', trigger: 'blur' }]
            },
            fileList: [],
            isUpdate: false, //是否是更新
            flag: false
        };
    },
    methods: {
        // 选择姓名
        showTreeDialog(tit) {
            this.searchDialogTit = tit;
            this.showSearch = true;
        },
        // 树组件搜索结果返回
        searchBack(data) {
            console.log(data.backData, 'searchTreeBack');
            this.ruleForm.resignationUserName = data.backData.name;
            this.ruleForm.resignationUserId = data.backData.id;
            this.ruleForm.jobRole = data.backData.jobName;
            this.ruleForm.jobRoleId = data.backData.jobId;
            this.ruleForm.departName = data.backData.organizationName;
            this.ruleForm.departNo = data.backData.organizationName;
            this.showSearch = false;
        },
        closeTreeDialog(data) {
            this.showSearch = data.showDialog;
        },
        //新增
        resignationInsert() {
            appMain
                .resignationInsert(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.resetForm('ruleForm');
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        resignationUpdateById() {
            appMain
                .resignationUpdateById(this.ruleForm)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.resetForm('ruleForm');
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        resignationFindById(id) {
            appMain
                .resignationFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.ruleForm = res.data.data;
                    } else {
                        this.$message.error('服务器繁忙，请稍后再试');
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        },
        //根据id查询附件
        uploadFileInfoFindById(id) {
            appMain
                .uploadFileInfoFindById({}, id)
                .then(res => {
                    if (res.data.code === 20000) {
                        this.fileList = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                    this.flag = true;
                })
                .catch(err => {
                    console.log(err);
                });
        },
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.ruleForm.uploadReturns = this.fileList;
                    //alert('submit!');
                    if (!this.isUpdate) {
                        //添加文件列表
                        //新增
                        this.resignationInsert();
                    } else {
                        //添加文件列表
                        //更新
                        this.resignationUpdateById();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 上传文件返回
        dealFiles(data) {
            this.fileList = data.backData;
        }
    },
    components: {
        uploadFiles
    },
    mounted() {
        var div_height = window.screen.availHeight;
        var height = div_height - 100;
        this.containerHeight = height - 160;
        this.pageTitle = this.$router.currentRoute.meta.title;
    },
    created() {
        //如果有id过来就是修改的
        if (this.$route.query.id) {
            let id = this.$route.query.id;
            //是更新操作
            this.isUpdate = true;
            //通过id查询对应离职信息
            this.resignationFindById(id);
            //通过id查询附件信息
            this.uploadFileInfoFindById(id);
        } else {
            //新增操作
            this.isUpdate = false;
            this.flag = true;
        }
    }
};
</script>

<style  scoped>
.container {
    padding-bottom: 50px;
}
.handle-box {
    padding: 5px 50px;
    width: 100%;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0px;
    border-bottom: 1px solid #ddd;
    border-top: 1px solid #ddd;
    z-index: 1;
}

.table-form-box table {
    background-color: #666;
}
.table-form-box .h3 {
    font-size: 22px;
    text-align: center;
    font-weight: normal;
    padding: 20px 0;
}
.table-form-box table .tr-title td {
    font-size: 14px;
    line-height: 40px;
}
.table-form-box table td,
.table-form-box table th {
    background-color: #fff;
    padding: 4px;
    min-height: 40px;
    font-size: 12px;
}
.color-ff2200 {
    color: #ff2200;
}
</style>